from django.http import HttpResponseBadRequest, HttpResponse, HttpRequest, JsonResponse
import hashlib
from .models import NewsClass, NewsSeo, NewsPower, NewsList, NewsSysteam
from django.db.models import Q
from django.core.paginator import Paginator
import simplejson
import django.utils.timezone as timezone
# 提供查询sql语句的引入
from django.db.models.query import QuerySet


def newsclass_l(request):
	if request.method == 'POST':
		# request.params = request.GET
		return newsclass_add(request)
	# # 添加管理用户
	elif request.method == 'GET':
		# request.params = simplejson.loads(request.body)
		return newsclass_list(request)
	# # 更新用户信息
	elif request.method == 'PUT':
		return newsclass_edit(request)
	# # 删除管理用户： 逻辑删除
	elif request.method == 'DELETE':
		return newsclass_delete(request)

	else:
		return JsonResponse({"state": 0, "msg": "信息传递有问题"})


def newsclass_add(request):
	# return HttpResponse('aaaaadsgbsvb')
	info = request.POST
	names = info['names']
	superior_id = info['superior_id']
	creation_time = timezone.now()
	updata_time = timezone.now()
	print(superior_id, names)
	try:
		print("-----" * 5)
		item = NewsClass.objects.filter(Q(names=names) & Q(superior_id=superior_id))
		print("//////" * 100)
		print(item, item.query)
		if item:
			return JsonResponse({"state": 0, "msg": info['names'] + "已存在,不能重复添加"})
		else:
			# print("--------" * 100)

			if info['state']:
				state = info['state']
			else:
				state = 1

			# print("+++++++" * 100)
			try:
				qs = NewsClass.objects.create(names=info['names'], superior_id=info['superior_id'], classify=info['classify'], index=info['index'], levels=info['levels'], untitled=info['untitled'], creation_time=creation_time, updata_by=info['updata_by'], updata_time=updata_time, creation_template=info['creation_template'], content_template=info['content_template'], state=state, power_id=1, seo_id=1)
				# info.save()
				# return JsonResponse('', safe=False)
				return JsonResponse({"state": 1, "msg": qs.names + "添加成功"})

			except NewsClass.DoesNotExist:
				return JsonResponse({"state": 0, "msg": "不能添加"})

	except NewsClass.DoesNotExist:
		return JsonResponse({"state": 0, "msg": "信息错误"})


def newsclass_list(request):
	data = {}
	# 获取传递过来的page
	try:
		page = request.GET.get("page")
		# 如果没有值则默认给一个值
		if not page:
			page = 1
		# 保证传递过来的值是整形
		page_id = int(page)
		# 查询所有用户
		qs = NewsClass.objects.all().values()
		# 创建分页对象  qs 后面的值 是一个页面有多少数据
		paginator = Paginator(qs, 3)
		# 根据当前页码,确定返回的数据
		current_page = paginator.page(page_id)
		# print(current_page)

		data["num"] = 1
		data["msg"] = list(current_page)

		return JsonResponse(data, safe=False)
	except Exception as e:
		return JsonResponse({"state": 0, "msg": "错误传值"})


def newsclass_edit(request):
	# info = request.body
	info = simplejson.loads(request.body)
	ids = info["newdata"]["id"]
	newsdata = info["newdata"]
	print(ids)
	try:
		customer = NewsClass.objects.get(pk=ids)
		print(customer)
	except NewsClass.DoesNotExist:
		return JsonResponse({"state": 0, "msg": "信息存在,不能重复添加"})
	try:
		if 'names' in newsdata:
			customer.names = newsdata['names']
		if 'superior_id' in newsdata:
			customer.superior_id = newsdata['superior_id']
		if 'updata_time' in newsdata:
			customer.updata_time = newsdata['updata_time']
		if 'classify' in newsdata:
			customer.classify = newsdata['classify']
		customer.update_time = timezone.now()
		customer.save()
		return JsonResponse({"state": 1, "msg": "修改成功"})
	except NewsClass.DoesNotExist:
		return JsonResponse({"state": 0, "msg": "修改出错"})


def newsclass_delete(request):
	info = simplejson.loads(request.body)
	if len(info) == 1:
		ids = info['id']
		stateid = 3
	else:
		ids = info['id']
		stateid = info['state']

	try:
		delete_users = NewsClass.objects.get(pk=ids)
		# print(delete_user)
	except NewsClass.DoesNotExist:
		return JsonResponse({"state": 0, "msg": "用户不存在"})

	if stateid == 1 or stateid == 0:
		delete_users.status = stateid
		if stateid == 1:
			delete_users.delete_time is None
		delete_users.save()
		return JsonResponse({"state": 1, "msg": "修改成功"})
	else:

		if delete_users.delete_time is not None:
			return JsonResponse({"state": 0, "msg": "用户已经删除"})
		else:
			# customer.delete()
			delete_users.state = 0
			# 采用逻辑删除
			delete_users.delete_time = timezone.now()
			delete_users.save()
			return JsonResponse({"state": 1, "msg": "删除成功"})